package com.tairanchina.wiki.api.model.wiki.content.module;

import com.tairanchina.wiki.api.TextBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 周靖捷 on 2016/10/6.
 */
public class LinearFragmentModule implements FragmentModule {

    List<FragmentModule> moduleList = new ArrayList<FragmentModule>();

    Boolean useSerialNumber = false;

    String content;

    public String getContent() {
        return content;
    }

    public LinearFragmentModule setContent(String content) {
        this.content = content;
        return this;
    }

    public Boolean isUseSerialNumber() {
        return useSerialNumber;
    }

    public LinearFragmentModule setUseSerialNumber(Boolean useSerialNumber) {
        this.useSerialNumber = useSerialNumber;
        return this;
    }

    public List<FragmentModule> getModuleList() {
        return moduleList;
    }

    public LinearFragmentModule setModuleList(List<FragmentModule> moduleList) {
        this.moduleList = moduleList;
        return this;
    }

    public LinearFragmentModule addModule(FragmentModule module) {
        moduleList.add(module);
        return this;
    }

    public Boolean getUseSerialNumber() {
        return useSerialNumber;
    }

    public LinearFragmentModule clear() {
        moduleList.clear();
        return this;
    }

    @Override
    public String toMarkdown() {
        TextBuilder tBuilder = new TextBuilder();
        if(content!=null&&content.length()!=0){
            tBuilder.writeln(content);
        }
        int i = 0;
        for (FragmentModule fragmentModule : moduleList) {
            i++;
            if (!useSerialNumber) {
                tBuilder.nextLine().indent("* " + fragmentModule.toMarkdown(), 1);
            } else {
                tBuilder.nextLine().indent(i + ". " + fragmentModule.toMarkdown(), 1);
            }
        }
        return tBuilder.build();
    }


    public static void main(String[] args) {
        LinearFragmentModule linearFragmentModule = new LinearFragmentModule();
        linearFragmentModule.addModule(new TextFragmentModule().setText("test1"))
                .addModule(new TextFragmentModule().setText("test2"))
                .addModule(new LinearFragmentModule().setContent("testSub").addModule(new TextFragmentModule().setText("sub1")).setUseSerialNumber(true))
                .setUseSerialNumber(true);
        System.out.println(linearFragmentModule.toMarkdown());
    }
}
